package com.sdtw.bookshop.shop.controller;

import com.sdtw.bookshop.shop.bean.BookInfo;
import com.sdtw.bookshop.shop.service.BookClassService;
import com.sdtw.bookshop.shop.service.BookKindService;
import com.sdtw.bookshop.shop.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
//@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BookClassService bookClassService;
    @Autowired
    private BookKindService bookKindService;
    // 服务启动自动跳转首页,只用于页面跳转
    @RequestMapping(value = {"/a"})
    public String index() {
        return "index";
    }
    @RequestMapping(value = "/find", produces = {"application/json;charset=utf-8"})
    public String findAll(Model mv) {
        mv.addAttribute("BOOK", bookService.findAll());
        return "merchandiseInformationList.htm";
    }

    @RequestMapping(value = "bookAdd", produces = {"application/json;charset=utf-8"})
    public String addBook(BookInfo bookInfo) {
        bookService.addBook(bookInfo);
        return "merchandiseInformationList.htm";
    }
    @RequestMapping(value = "/update", produces = {"application/json;charset=utf-8"})
    public String update(Integer id,Model mv1,Model mv2) {
        bookService.updateBookInfo(id);
        mv1.addAttribute("BOOKCLASS",bookClassService.findAll());
        mv2.addAttribute("BOOKKIND",bookKindService.findAll());
        System.out.println(bookClassService.findAll());
        System.out.println(bookKindService.findAll());
        return "merchandiseInformationList.htm";//返回到更新页面，并且需要将选中的信息携带到更新页面（findByBookId）
    }

    @RequestMapping(value = "/delete", produces = {"application/json;charset=utf-8"})
    public String deleteBookInfo(Integer id) {
        bookService.deleteBookInfo(id);
        return "redirect:/find";//点击删除，页面内刷新
    }
    @RequestMapping(value = "/findByBookId", produces = {"application/json;charset=utf-8"})
    public String deleteBookInfo1(Integer id,Model model,Model mv1,Model mv2) {
        model.addAttribute("UPDATEBOOK",bookService.findByBookId(id));
        mv1.addAttribute("BOOKCLASS",bookClassService.findAll());
        mv2.addAttribute("BOOKKIND",bookKindService.findAll());
        return "merchandiseInformationModify.htm";
    }
}
